﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;

using WXDba.Entity;
using WXDba.Abstract;

namespace WXPackage
{
    /// <summary>
    /// 接收消息，管理
    /// </summary>
    public class ReceiveMessage : BaseManage
    {
        IMsgRecord _IMsgRecord = ConcreteFactory.GetMsgRecord();
        /// <summary>
        /// 当前消息对象
        /// </summary>
        MsgRecord _msg = null;
        public ReceiveMessage() { }
        /// <summary>
        /// 获取当前消息对象
        /// </summary>
        /// <returns></returns>
        public MsgRecord GetCurrent()
        {
            return _msg;
        }
        /// <summary>
        /// 指定消息内容，处理
        /// </summary>
        /// <param name="xmlMsg"></param>
        public ReceiveMessage(string xmlMsg)
        {
            _msg = LoadFromXml(xmlMsg);
        }
        /// <summary>
        /// 解析接收到的消息
        /// </summary>
        /// <param name="msg">消息内容</param>
        /// <returns></returns>
        public MsgRecord LoadFromXml(string msg)
        {
            XmlDocument doc = new XmlDocument();
            doc.LoadXml(msg);
            XmlNode xml = doc.SelectSingleNode("xml");
            if (xml == null)
                throw new Exception("读取消息内容时，获取xml节点失败");
            //获取基本属性
            MsgRecord _msg = new MsgRecord();
            _msg.ToUserName = xml.SelectSingleNode("ToUserName").InnerText;
            _msg.FromUserName = xml.SelectSingleNode("FromUserName").InnerText;
            DateTime create = new DateTime(1970,1,1).AddSeconds(Convert.ToInt64(xml.SelectSingleNode("CreateTime").InnerText));
            _msg.CreateTime = create;
            //消息类型
            string type = xml.SelectSingleNode("MsgType").InnerText;
            _msg.MsgType = type;
            //消息ID
            XmlNode msgID_node = xml.SelectSingleNode("MsgId");
            if (msgID_node != null)
            {
                _msg.MsgID = Convert.ToInt64(msgID_node.InnerText);
            }
            //根据消息类型设置消息内容
            if (type.Equals("text"))
            {
                _msg.Content = xml.SelectSingleNode("Content").InnerText;
            }
            else if (type.Equals("image"))
            {
                _msg.MediaId = xml.SelectSingleNode("MediaId").InnerText;
                _msg.Content = xml.SelectSingleNode("PicUrl").InnerText;
            }
            else if (type.Equals("link"))
            {
                XmlElement elem = doc.CreateElement("xml");
                elem.AppendChild(doc.SelectSingleNode("Title"));
                elem.AppendChild(doc.SelectSingleNode("Description"));
                elem.AppendChild(doc.SelectSingleNode("Url"));
                StringBuilder builder = new StringBuilder();
                using (XmlWriter xw = XmlWriter.Create(builder))
                {
                    elem.WriteTo(xw);
                    _msg.Content = builder.ToString();
                }
            }
            else
            {
                throw new Exception("暂时不支持，当前类型解析," + type);
            }
            try
            {
                //将数据保存到数据库
                SaveMsg(_msg);
            }
            catch (Exception ex)
            {
                throw new Exception("将接收到的消息保存数据库失败，" + ex.Message);
            }
            return _msg;
        }
        /// <summary>
        /// 将消息内容添加到数据库
        /// </summary>
        private void SaveMsg(MsgRecord record)
        {
            //1.判断当前msgid的消息是否已经存在
            if (record.MsgID != null && _IMsgRecord.Exists(record.MsgID.Value))
            {
                return;
            }
            record.AddTime = DateTime.Now;
            _IMsgRecord.Add(record);
        }
    }
}
